<template>
    <!--
        部门选择器
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
    -->
    <b-modal ref="my-modal" title="上级资源" scrollable hide-footer no-close-on-backdrop>
        <div class="d-block text-center">
            <div class="m-form m-form--fit m-form--label-align-left m-form--group-seperator-dashed">
                <div class="m-portlet__body">
                    <div class="form-group m-form__group row">
                        <label class="col-form-label">隶属平台:</label>
						<div class="col-lg-6">
                            <el-select v-model="sysModeId">
                                <el-option
                                v-for="item in sysModeList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                                </el-option>
                            </el-select>
						</div>
                    </div>
                    <div class="form-group m-form__group row">
                        <div class="col-md-12">
                        <el-tree 
                            :data="resourcesTreeDataList" 
                            ref="tree"
                            node-key="id"
                            :props="defaultProps" 
                            @node-click="doSelect"></el-tree>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <!-- <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取消
            </button> 
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="doSelect">
                <i class="icon-xl fas fa-save"></i> 确定
            </button> -->
        </template>

    </b-modal>
</template>

<script>
    import apiUtil from "@/core/util/apiUtil.js"; 
    import { handleNotify,handleAlert,handleConfirm,showWating,closeWating} from "@/core/util/jehcUtil.js";
    export default {
        data() {
            return {
                resourcesTreeDataList:[],
                sysModeId:"",
                id:"",
                childNodeIds:[],
                sysModeList:[],
                defaultProps: {
                    children: 'children',
                    label: 'text'
                }
            }
        },
        watch:{
            sysModeId(newVal,oldVal) {
                this.initResourcesTreeDataList(newVal);
            },
        },
        mounted() {
            this.initSysModeList();
        },
        methods: {              
            showModal(id) {
                this.id = id;
                this.$refs['my-modal'].show();
                this.initResourcesTreeDataList();
            },
            hideModal() {
                this.$refs['my-modal'].hide()
            },
            toggleModal() {
                this.$refs['my-modal'].toggle('#toggle-btn')
            },
            doSelect(data){
                this.childNodeIds = []; 
                if(this.id === data.id){
                    handleAlert("不能选择自己", "warning", 3)
                    return;
                }                   
                if(this.id !== data.id){
                     //1.找到当前节点所在的层级
                    const node = this.$refs.tree.getNode(this.id);
                    if(node && null != node){
                        //2.判断所选择的节点是否在当前层级子孙级 如果存在则提示不能选择 否则合法
                        this.getChildAllNodes(node);  //获取所有其下节点          
                        let childNodeIds = this.childNodeIds;//节点集合
                        if(childNodeIds){
                            for(var i in childNodeIds){                            
                                if(childNodeIds[i] === data.id){
                                    handleAlert("您选择的上级资源不合法！", "warning", 3)
                                    return;
                                }
                            }
                        }
                    }
                    
                }
                this.$emit("change",data);//组件传值，即向父级模态框中传递值，采用change方式
                // let id = data.id;
                // console.log(data);
                this.$refs['my-modal'].hide();
            },
            getChildAllNodes(node) {//查询初始化树中选择节点id                
                let nodes = node.childNodes;
                for(let item of nodes) {
                    this.childNodeIds.push(item.key);
                    if(item.children){                        
                        this.getChildAllNodes(item);
                    }
                }
            },         
            initResourcesTreeDataList(value){
                if(value && null != value && "" != value){
                    let params = {"sysModeId":value};
                    apiUtil.query(process.env.VUE_APP_OAUTH_API+"/oauthResources/bZTree", params).then(({ data }) => {
                        let result = JSON.parse(data.data);
                        this.resourcesTreeDataList = result;     
                    });
                }
            },
            initSysModeList(){
                let params = {};
                apiUtil.query(process.env.VUE_APP_OAUTH_API+"/oauthSysMode/listAll", params).then(({ data }) => {
                    this.sysModeList = data.data;
                });
            }
        }
    }
</script>